home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / databasehelper.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  6.3 KB  |  201 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. from itertools import count
  5. import database
  6.  
  7. def makeSimpleGetSet(attributeName, changeNeedsSave = True):
  8.     '''Creates a simple DDBObject getter and setter for an attribute.
  9.  
  10.     This exists because for many DDBOBject attributes we have methods like the
  11.     following:
  12.  
  13.     def getFoo(self):
  14.         self.confirmDBThread()
  15.         return self.foo
  16.     def setFoo(self, newFoo):
  17.         self.confirmDBThread()
  18.         self.foo = newFoo
  19.         self.signalChange()
  20.     '''
  21.     
  22.     def getter(self):
  23.         self.confirmDBThread()
  24.         return getattr(self, attributeName)
  25.  
  26.     
  27.     def setter(self, newValue):
  28.         self.confirmDBThread()
  29.         setattr(self, attributeName, newValue)
  30.         self.signalChange(needsSave = changeNeedsSave)
  31.  
  32.     return (getter, setter)
  33.  
  34.  
  35. class TrackedIDList(object):
  36.     '''Creates a view that tracks a list of DDBObject IDs.
  37.  
  38.     The view will have the corresponding DDBObject for each ID in the list and
  39.     will be in the same order.
  40.  
  41.     Attributes:
  42.  
  43.     view -- Database view that tracks the list of ids
  44.     '''
  45.     
  46.     def __init__(self, db, idList, sortFunc = None):
  47.         '''Construct an TrackedIDList.  
  48.  
  49.         This object will keep a reference to idList.  When insertID, appendID,
  50.         removeID, or moveID are called idList will be modified to
  51.         reflect the change.
  52.         '''
  53.         if sortFunc == None:
  54.             sortFunc = self.sort
  55.         
  56.         self.trackedIDs = set()
  57.         self.positions = { }
  58.         self.list = idList
  59.         self.db = db
  60.         pos = count()
  61.         for id in idList:
  62.             self.positions[id] = pos.next()
  63.             self.trackedIDs.add(id)
  64.         
  65.         
  66.         self.extraFilterFunc = lambda x: True
  67.         self.filter1 = db.filter(self.filter)
  68.         self.filter2 = self.filter1.filter(self.extraFilter)
  69.         self.view = self.filter2.sort(sortFunc, resort = True)
  70.  
  71.     
  72.     def recomputeSort(self):
  73.         self.filter2.recomputeSort(self.view)
  74.  
  75.     
  76.     def sort(self, a, b):
  77.         return self.positions[a[1].getID()] < self.positions[b[1].getID()]
  78.  
  79.     
  80.     def filter(self, object):
  81.         return object.getID() in self.trackedIDs
  82.  
  83.     
  84.     def extraFilter(self, object):
  85.         return self.extraFilterFunc(object)
  86.  
  87.     
  88.     def setFilter(self, extraFilterFunc):
  89.         self.extraFilterFunc = extraFilterFunc
  90.         self.filter1.recomputeFilter(self.filter2)
  91.  
  92.     
  93.     def _sendSignalChange(self, id):
  94.         if self.db.idExists(id):
  95.             self.db.getObjectByID(id).signalChange(needsSave = False)
  96.         
  97.  
  98.     
  99.     def __contains__(self, id):
  100.         return id in self.trackedIDs
  101.  
  102.     
  103.     def getPosition(self, id):
  104.         '''Get the position of an id in the list.  If id is not in the list a
  105.         KeyError will be raised.'''
  106.         return self.positions[id]
  107.  
  108.     
  109.     def appendID(self, id, sendSignalChange = True):
  110.         if id in self:
  111.             raise ValueError('%s is already being tracked' % id)
  112.         
  113.         self.positions[id] = len(self.list)
  114.         self.trackedIDs.add(id)
  115.         self.list.append(id)
  116.         if sendSignalChange:
  117.             self._sendSignalChange(id)
  118.         
  119.  
  120.     
  121.     def insertID(self, pos, id):
  122.         if id in self:
  123.             raise ValueError('%s is already being tracked' % id)
  124.         
  125.         for toMove, oldPos in self.positions.items():
  126.             if oldPos >= pos:
  127.                 self.positions[toMove] += 1
  128.                 continue
  129.         
  130.         self.positions[id] = pos
  131.         self.list.insert(pos, id)
  132.         self.trackedIDs.add(id)
  133.         self._sendSignalChange(id)
  134.  
  135.     
  136.     def removeID(self, id):
  137.         removedPos = self.positions.pop(id)
  138.         for toMove, oldPos in self.positions.items():
  139.             if oldPos > removedPos:
  140.                 self.positions[toMove] -= 1
  141.                 continue
  142.         
  143.         del self.list[removedPos]
  144.         self.trackedIDs.remove(id)
  145.         self._sendSignalChange(id)
  146.  
  147.     
  148.     def moveID(self, id, pos):
  149.         """Move an id from it's current position to a new position, shifting
  150.         the rest of the list to accomidate it.
  151.         """
  152.         currentPos = self.positions[id]
  153.         if currentPos > pos:
  154.             minChange = pos
  155.             maxChange = currentPos - 1
  156.             delta = 1
  157.         else:
  158.             minChange = currentPos + 1
  159.             maxChange = pos
  160.             delta = -1
  161.         for toMove, oldPos in self.positions.items():
  162.             if oldPos <= oldPos:
  163.                 pass
  164.             elif oldPos <= maxChange:
  165.                 self.positions[toMove] = oldPos + delta
  166.                 continue
  167.         
  168.         self.positions[id] = pos
  169.         del self.list[currentPos]
  170.         self.list.insert(pos, id)
  171.         self._sendSignalChange(id)
  172.  
  173.     
  174.     def moveIDList(self, idList, anchorID):
  175.         """Move a set of IDs to be above anchorID.
  176.  
  177.         More precicely, we move idList so it is one contiguous block, in
  178.         between anchorID and the first id not in idList.  The ids in idList
  179.         won't change position relative to each other.
  180.  
  181.         If anchorID is None, idList will be positioned at the bottom.
  182.         """
  183.         if anchorID in idList:
  184.             return None
  185.         
  186.         toMove = [ (self.getPosition(id), id) for id in idList ]
  187.         toMove.sort()
  188.         for oldPos, id in toMove:
  189.             self.removeID(id)
  190.         
  191.         for oldPos, id in toMove:
  192.             if anchorID is not None:
  193.                 anchorPos = self.getPosition(anchorID)
  194.                 self.insertID(anchorPos, id)
  195.                 continue
  196.             []
  197.             self.appendID(id)
  198.         
  199.  
  200.  
  201.